package com.clayecloud.common.admin.services.Impl;

import com.clayecloud.common.admin.entity.AdminRole;
import com.clayecloud.common.admin.entity.AdminUser;
import com.clayecloud.common.admin.services.RoleService;
import com.clayecloud.common.entity.Role;
import com.clayecloud.common.entity.Visitor;
import com.clayecloud.common.mappers.RoleMapper;
import com.clayecloud.common.utils.MessageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ClayeLink
 * @date 2022/3/16 22:24
 */
@Service("AdminRoleServiceImpl")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public MessageUtil selectAllForUser() {
        List<Role> roles = roleMapper.selectAll();
        List<AdminRole> adminRoles = new ArrayList<>();
        List<AdminRole> adminRolesSon = new ArrayList<>();
        for(Role role : roles){
            adminRolesSon.add(new AdminRole(role.getRoleId(), role.getRoleName(), null));
        }
        adminRoles.add(new AdminRole(0, "系统角色列表", adminRolesSon));
        return MessageUtil.success("查询成功").add(adminRoles);
    }

    @Override
    public MessageUtil insert(Role role) {
        return roleMapper.insert(role) > 0 ? MessageUtil.success("增加成功") : MessageUtil.fail("增加失败，请联系系统管理员");
    }

    @Override
    public MessageUtil delete(Integer roleID) {
        return roleMapper.deleteByPrimaryKey(roleID) > 0 ? MessageUtil.success("删除成功") : MessageUtil.fail("删除失败，请联系系统管理员");
    }

    @Override
    public MessageUtil update(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role) > 0 ? MessageUtil.success("修改成功") : MessageUtil.fail("修改失败，请联系系统管理员");
    }

    @Override
    public MessageUtil selectAll(Integer size, Integer page) {
        PageHelper.startPage(page,size);
        List<Role> roles = roleMapper.selectAll();
        PageInfo pageInfo = new PageInfo(roles);
        Map result = new HashMap();
        result.put("list",roles);
        result.put("total",pageInfo.getTotal());
        return MessageUtil.success("查询成功").add(result);
    }
}
